package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Comment;
import com.example.demo.entity.UserAward;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.service.CommentService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;
    public int userComment(@RequestBody UserAward userAward, String content, String image, String commentDate, Integer score) throws ParseException{
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date date = ft.parse(commentDate);
        Comment comment = new Comment();
        comment.setOrderId(userAward.getOrderId());
        comment.setCommentDate(date);
        comment.setOpenId(userAward.getOpenId());
        comment.setParentId(0);
        comment.setContent(content);
        comment.setImage(image);
        comment.setBusinessId(userAward.getBusinessId());
        comment.setGoodId(userAward.getGoodId());
        comment.setScore(score);
        return commentMapper.insert(comment);
    }
    public int userReply(Comment comment, String reply, String image, String date, String openId) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date commentDate = ft.parse(date);
        Comment comment1= new Comment();
        comment1.setOrderId(comment.getOrderId());
        comment1.setCommentDate(commentDate);
        comment1.setOpenId(openId);
        comment1.setParentId(comment.getParentId());
        comment1.setContent(reply);
        comment.setImage(image);
        comment1.setBusinessId(null);
        comment1.setGoodId(comment.getGoodId());
        comment1.setScore(null);
        return commentMapper.insert(comment1);
    }
    public int businessReplyComment(Integer commentId, String reply,String image, String date) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date commentDate = ft.parse(date);
        Comment comment = commentMapper.selectById(commentId);
        Comment comment1 = new Comment();
        comment1.setContent(reply);
        comment1.setImage(image);
        comment1.setCommentDate(commentDate);
        comment1.setParentId(comment.getCommentId());
        comment1.setOrderId(comment.getOrderId());
        comment1.setOpenId(null);
        comment1.setGoodId(comment.getGoodId());
        comment1.setBusinessId(comment.getBusinessId());
        comment1.setScore(null);
        return commentMapper.insert(comment1);
    }

    public Comment getOrderComment(String orderId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_id",orderId);
        queryWrapper.eq("parent_id",0);
        Comment comment = commentMapper.selectOne(queryWrapper);
        if(comment==null){
            return comment;
        }
        Integer commentId = comment.getCommentId();
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("parent_id",commentId);
        List<Comment> reply = commentMapper.selectList(queryWrapper1);
        if(!reply.isEmpty()){
            comment.setChildren(getCommentChildren(orderId,reply));;
        }
        return comment;
    }

    public List<Comment> getCommentChildren(String orderId, List<Comment> commentList){
        for(Comment comment : commentList){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("order_id",comment.getOrderId());
            queryWrapper.eq("parent_id",comment.getCommentId());
            List<Comment> reply = commentMapper.selectList(queryWrapper);

            if(!reply.isEmpty()){
                comment.setChildren(getCommentChildren(comment.getOrderId(),reply));
            }
        }
        return commentList;
    }

    @Resource
    private GoodsMapper goodsMapper;
    public List<Comment> getCommentList(Integer businessId, String startTime, String endTime,
                                        String goodName, Integer score, Integer ifHaveReply) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        if(startTime != null && !startTime.isEmpty()){
            startDate = formatter.parse(startTime);
        }
        if(endTime != null && !endTime.isEmpty()){
            endDate = formatter.parse(endTime);
        }
        List<Comment> commentList = commentMapper.getCommentSearch(businessId, startDate, endDate, score);
        if(ifHaveReply != null){
            if(ifHaveReply == 1){ // 未回复
                commentList=commentList.stream().filter(comment->comment.getChildren()!=null).collect(Collectors.toList());
            }
            if(ifHaveReply == 0){ //已回复
                for(Comment comment : commentList){
                    if(comment.getChildren() != null){
                        commentList.remove(comment);
                    }
                }
            }
            if(commentList.isEmpty()){
                return null;
            }
        }
        List<Integer> goodIdList = goodsMapper.getGoodIdList(goodName);
        commentList = commentList.stream()
                .filter(comment -> goodIdList.contains(comment.getGoodId()))
                .collect(Collectors.toList());
        return commentList;
    }

    public Integer deleteComment(Integer commentId){
        return commentMapper.deleteById(commentId);
    }

}
